﻿using System.Collections.Generic;
using System.Threading.Tasks;
using SqlSugar;

namespace PMP.Core.SqlSugar;

public static class PageExtension
{
    public static Page<T> ToPage<T>(this ISugarQueryable<T> queryable, int pageIndex, int pageSize) where T : class
    {
        int totalCount = 0;
        var res = queryable.ToPageList(pageIndex, pageSize, ref totalCount);
        return new Page<T>()
        {
            Items = res,
            PageIndex = pageIndex,
            PageSize = pageSize,
            TotalCount = totalCount
        };
    }
    
    public static async Task<Page<T>> ToPageAsync<T>(this ISugarQueryable<T> queryable, int pageIndex, int pageSize) where T : class
    {
        RefAsync<int> totalCount = 0;
        var res = await queryable.ToPageListAsync(pageIndex, pageSize, totalCount );
        return new Page<T>()
        {
            Items = res,
            PageIndex = pageIndex,
            PageSize = pageSize,
            TotalCount = totalCount.Value
        };
    }
    
}


/// <summary>
/// 分页结果
/// </summary>
/// <typeparam name="T"></typeparam>
public class Page<T> where T : class
{
    public int PageIndex { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }
    public List<T> Items { get; set; }
}
